import Vue from 'vue';
import App from './App.vue';
import ElementUI from 'element-ui';
import axios from 'axios';
import 'element-ui/lib/theme-chalk/index.css';
import { Message, Loading } from 'element-ui';
import router from './router/index.js';
import VueI18n from 'vue-i18n';
import { messages } from './store/messages';
import store from './store/store';
Vue.use(VueI18n);

const i18n = new VueI18n({
    locale: store.state.locale, // 设置默认语言
    messages, // 设置语言信息
});

Vue.config.productionTip = false;

Vue.use(ElementUI);

Vue.prototype.fiex = (num, decimal) => {
    num = num.toString();
    let index = num.indexOf('.');
    if (index !== -1) {
        num = num.substring(0, decimal + index + 1);
    } else {
        num = num.substring(0);
    }
    return parseFloat(num).toFixed(decimal);
};

//封装公共AJAX函数
Vue.prototype.ajaxs = (url, obj, headers = { 'Content-Type': '' }) => {
    var data = obj.data ? obj.data : {};
    axios({
        method: 'POST',
        url: '/api/' + url,
        data: data,
        headers:
            headers['Content-Type'] == ''
                ? {
                      'Content-Type': 'application/json',
                      'x-requested-with': 'XMLHttpRequest',
                  }
                : headers,
    })
        .then(res => {
            res = res.data;
            obj.success(res);
        })
        .catch(err => {
            Message.error(err);
        })
        .catch(e => {
            Message.error(e);
        });
};

//封装网页弹窗
Vue.prototype.toast = (code, msg, func = () => {}) => {
    if (code == 1) {
        Message.success(msg);
    }
    if (code == 0) {
        Message.warning(msg);
    }
    setTimeout(func, 1000);
};
// router.beforeEach((to, from, next) => {
//     if (to.path === '/') {
//         next('/'); // 重定向到/home
//     } else {
//         next(); // 继续
//     }
// });
new Vue({
    store,
    i18n,
    router,
    render: h => h(App),
}).$mount('#app');
// router.isReady().then(() => {});
